<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span>{{ "▎ " + showTitle }}</span>
      <span class="fa fa-chevron-down title-icon" :style="comStyle" @click="showChoice = !showChoice"></span>
      <div class="select-con" v-show="showChoice" :style="marginStyle">
        <div class="select-item" v-for="item in selectTypes" :key="item.key" @click="handleSelect(item.key)">
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>

<script>
import { getTrendData } from "../api"
export default {
  name: "Trend",
  data() {
    return {
      chartInstance: null, // echarts实例
      allData: {},
      showChoice: false, // 是否显示可选项的div
      choiceType: "map", // 显示的数据类型
      fontsize: null,
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener("resize", this.adaptChart)
    this.adaptChart()
  },
  destroyed() {
    window.removeEventListener("resize", this.adaptChart)
  },
  methods: {
    // 初始化echarts
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.trend_ref, "chalk")

      // 设置option
      const option = {
        legend: {
          left: 20,
          top: "15%",
          icon: "circle", // 图例设置为圆形
        },
        grid: {
          // 坐标轴的配置
          left: "3%",
          top: "35%",
          right: "4%",
          bottom: "1%",
          containLabel: true, // 给网格设置位置时包含文字的设置
        },
        tooltip: {
          // 提示
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false, // x轴上的线靠近边线
        },
        yAxis: {
          type: "value",
        },
      }

      this.chartInstance.setOption(option)
    },
    // 获取数据
    async getData() {
      const res = await getTrendData()
      console.log(res)
      this.allData = res
      this.updateChart()
    },
    // 更新数据
    updateChart() {
      // 半透明的颜色值
      const colorArr1 = ["rgba(11, 168, 44, 0.5)", "rgba(44, 110, 255, 0.5)", "rgba(22, 242, 217, 0.5)", "rgba(254, 33, 30, 0.5)", "rgba(250, 105, 0, 0.5)"]
      // 全透明的颜色值
      const colorArr2 = ["rgba(11, 168, 44, 0)", "rgba(44, 110, 255, 0)", "rgba(22, 242, 217, 0)", "rgba(254, 33, 30, 0)", "rgba(250, 105, 0, 0)"]
      // y轴数据
      const yArr = this.allData[this.choiceType].data.map((item, index) => {
        return {
          name: item.name,
          type: "line",
          data: item.data,
          stack: this.choiceType, // 以堆叠图的形式展现
          areaStyle: {}, // 堆叠图中填充颜色
          color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
            // 百分之0状态之下的颜色值
            {
              offset: 0,
              color: colorArr1[index],
            },
            // 百分之100状态之下的颜色值
            {
              offset: 1,
              color: colorArr2[index],
            },
          ]),
        }
      })
      // 图例的数据
      const legendArr = this.allData[this.choiceType].data.map((item) => item.name)
      const option = {
        legend: {
          data: legendArr,
        },
        xAxis: {
          data: this.allData.common.month,
        },
        series: yArr,
      }
      this.chartInstance.setOption(option)
    },
    // 分辨率适配
    adaptChart() {
      // 随着屏幕的大小改变而改变
      this.fontsize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6
      const option = {
        legend: {
          itemWidth: this.fontsize,
          itemHeight: this.fontsize,
          itemGap: this.fontsize,
          textStyle: {
            fontsize: this.fontsize / 2,
          },
        },
      }
      this.chartInstance.setOption(option)
      this.chartInstance.resize()
    },
    handleSelect(val) {
      this.choiceType = val
      this.showChoice = false
      this.updateChart()
    },
  },
  computed: {
    showTitle() {
      if (!this.allData[this.choiceType]) {
        return ""
      } else {
        return this.allData[this.choiceType].title
      }
    },
    // 可选择类型的数组
    selectTypes() {
      return this.allData.type ? this.allData.type.filter((item) => item.key != this.choiceType) : []
    },
    // 设置给标题的样式
    comStyle() {
      return {
        fontSize: this.fontsize + "px",
      }
    },
    marginStyle() {
      return {
        marginLeft: this.fontsize + "px",
      }
    },
  },
}
</script>
<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: white;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  .select-con {
    background-color: #222733;
  }
}
</style>
